package com.itheima.web.servlet.brand;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import com.itheima.web.servlet.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/9
 */
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private final BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Brand> list = brandService.selectAll();
        String brands = JSON.toJSONString(list);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(brands);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonBrand = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonBrand, Brand.class);
        brandService.addBrand(brand);
        response.getWriter().write("success");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonBrand = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonBrand, Brand.class);
        brandService.updateBrand(brand);
        response.getWriter().write("success");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonBrand = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonBrand, Brand.class);
        brandService.deleteBrand(brand.getId());
        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonBrand = request.getReader().readLine();
        int[] ids = JSON.parseObject(jsonBrand, int[].class);
        /*List list = JSON.parseObject(jsonBrand, List.class);
        int[] ids = new int[list.size()];
        for (int i = 0; i < ids.length; i++) {
            Object o = list.get(i);
            String s = JSON.toJSONString(o);
            Brand brand = JSON.parseObject(s, Brand.class);
            ids[i] = brand.getId();
        }*/
        brandService.deleteByIds(ids);
        response.getWriter().write("success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);
        String jsonPageBean = JSON.toJSONString(pageBean);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(jsonPageBean);
    }

    public void selectByConditionAndPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接收拼接参数 --> currentPage、pageSize --> request.getParameter()
        Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        //接收JSON参数 --> brand --> request.getReader().readLine()
        String jsonBrand = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonBrand, Brand.class);
        //将brand, currentPage, pageSize传入selectByConditionAndPage()进行查询逻辑
        PageBean<Brand> pageBean = brandService.selectByConditionAndPage(brand, currentPage, pageSize);
        String pageBeanJson = JSON.toJSONString(pageBean);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(pageBeanJson);
    }

}
